package com.lxw.report.meta.query.dbpool;

import com.lxw.report.meta.Exception.CommonException;
import com.lxw.report.meta.entity.DataBase;
import com.lxw.report.meta.enums.DbTypeEnum;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;


public class HiKariCpDataSourcePool implements DataSourcePoolWrapper {
    private static Logger logger = LoggerFactory.getLogger(HiKariCpDataSourcePool.class);

    @Override
    public DataSource wrap( DataBase dataBase) throws CommonException {
        logger.info("begin HiKariCpDataSourcePool create...");
        try {
             HikariDataSource dataSource = new HikariDataSource();
            if(DbTypeEnum.getDriverByName(dataBase.getType())==null){
                throw new CommonException("未找到对应的数据库驱动："+ dataBase.getType());
            }
            dataSource.setDriverClassName(DbTypeEnum.getDriverByName(dataBase.getType()));
            dataSource.setJdbcUrl(dataBase.getJdbcUrl());
            dataSource.setUsername(dataBase.getUser());
            dataSource.setPassword(dataBase.getPassword());
            return dataSource;
        } catch ( Exception ex) {
            throw new CommonException("连接池创建失败："+ ex.getMessage());
        }
    }
}
